{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "随机投影"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(100, 3947)\n",
      "47.5608940283334\n",
      "(100, 3947)\n",
      "43.819210159457796\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from scipy.spatial import distance\n",
    "\n",
    "X = np.random.rand(100, 10000)\n",
    "D1 = distance.cdist(X, X, 'euclidean')\n",
    "\n",
    "transformer = random_projection.GaussianRandomProjection()\n",
    "XX = transformer.fit_transform(X)\n",
    "print(XX.shape)\n",
    "D2 = distance.cdist(XX, XX, 'euclidean')\n",
    "\n",
    "print(np.linalg.norm(D1 - D2, ord='fro'))\n",
    "\n",
    "transformer = random_projection.SparseRandomProjection()\n",
    "XX = transformer.fit_transform(X)\n",
    "print(XX.shape)\n",
    "D2 = distance.cdist(XX, XX, 'euclidean')\n",
    "\n",
    "print(np.linalg.norm(D1 - D2, ord='fro'))\n"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 64-bit",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
